package com.we823.project.repository.service.we;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.we823.project.model.we.PermissionModel;
import com.we823.project.model.we.ResourceModel;
import com.we823.project.model.we.RoleResourcePermissionModel;
import com.we823.project.repository.dao.we.RoleResourcePermissionRepository;
import com.we823.project.repository.service.AbstractBaseService;

@Service("roleResourcePermissionService")
@Scope("prototype")
public class RoleResourcePermissionService extends AbstractBaseService<RoleResourcePermissionModel> {

	@Autowired
	private ResourceService resourceService;
	@Autowired
	private PermissionService permissionService;
	
	private RoleResourcePermissionRepository roleResourcePermissionRepository;
	
	@Autowired
	public RoleResourcePermissionService(RoleResourcePermissionRepository roleResourcePermissionRepository) {
		super(roleResourcePermissionRepository);
		this.roleResourcePermissionRepository=roleResourcePermissionRepository;
	}

	public List<RoleResourcePermissionModel> save(
			List<RoleResourcePermissionModel> roleResourcePermissionModels) {
		return roleResourcePermissionRepository.save(roleResourcePermissionModels);		
	}

	public List<RoleResourcePermissionModel> findByRoleid(int roleid) {
		return roleResourcePermissionRepository.findByRoleid(roleid);
	}

	public void update(List<RoleResourcePermissionModel> needUpdateList) {
		roleResourcePermissionRepository.save(needUpdateList);
		
	}

	public void delete(List<RoleResourcePermissionModel> needDeleteList) {
		roleResourcePermissionRepository.delete(needDeleteList);
	}

	public List<RoleResourcePermissionModel> findByRoleidIn(
			List<Integer> roleIdList) {
		return roleResourcePermissionRepository.findByRoleidIn(roleIdList);
	}

	public Set<String> findStringPermissionByRoleidIn(List<Integer> roleidList){
		Set<String> permissionSet = new HashSet<>();
		List<RoleResourcePermissionModel> roleResourcePermissionModels = findByRoleidIn(roleidList);
		if (roleResourcePermissionModels!=null && roleResourcePermissionModels.size()>0) {
			String permission = "";
			
			Map<Integer, ResourceModel> resouceMap = resourceService.map();
			Map<Integer, PermissionModel> permissionMap = permissionService.map();
			for (RoleResourcePermissionModel roleResourcePermissionModel : roleResourcePermissionModels) {
				
				String permissionIds = roleResourcePermissionModel.getPermissionIds();
				if(StringUtils.isBlank(permissionIds)) {
					break;
				}
				String[] permissionIdStrings = StringUtils.split(permissionIds,",");
				if(permissionIdStrings!=null && permissionIdStrings.length>0) {
					ResourceModel resourceModel = resouceMap.get(roleResourcePermissionModel.getResourceid());
					for (String permissionId : permissionIdStrings) {
						if(StringUtils.isNumeric(permissionId)) {
							int pId = Integer.parseInt(permissionId);
							if (pId>0) {
								PermissionModel permissionModel = permissionMap.get(pId);
								if(permissionModel!=null && resourceModel!=null) {
									permission = resourceModel.getIdentity()+":"+permissionModel.getPermission();
									permissionSet.add(permission);
								}
							}
						}
					}
				}
				
			}
		}
		return permissionSet;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public PermissionService getPermissionService() {
		return permissionService;
	}

	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}
	
	
}
